package backend;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

//All methods inheriting from Plugin must define check() and genCommandStr() methods
public abstract class Plugin implements Event {

    protected ArrayList<Server> serverList;
    private String name;
    //protected String command;
    private String output = "";

    public Plugin(String name, ArrayList<Server> s) {
        this.name = name;
        //command = "ssh spoon.netsoc.tcd.ie \"" + getCommandStr()+"\"";
        serverList = s;
    }

    public String runCommand(String command) {
        System.out.println(command);
        String[] cmd = {"sh", "-c", command};
        Runtime r = Runtime.getRuntime();
        StringBuffer temp = new StringBuffer();
        try {
            //Execute command
            Process p = r.exec(cmd);

            //------------Set up streams to get stdin---------------------//
            InputStream in = p.getInputStream();
            BufferedInputStream buf = new BufferedInputStream(in);
            InputStreamReader inread = new InputStreamReader(buf);
            BufferedReader bufferedreader = new BufferedReader(inread);

            String line;
            while ((line = bufferedreader.readLine()) != null) {
                temp.append(line);
            }

            setOutput(output + "\n" + temp.toString());
            //------------------------------------------------------------//

            //----------Set up streams to get stderr-----------------------//
            InputStream err = p.getErrorStream();
            BufferedInputStream buferr = new BufferedInputStream(err);
            InputStreamReader errread = new InputStreamReader(buferr);
            BufferedReader bufferedreadererr = new BufferedReader(errread);

            temp = new StringBuffer();
            while ((line = bufferedreadererr.readLine()) != null) {
                temp.append(line);
            }

            if (line != null) {
                System.err.println(line);
            }
            //---------------------------------------------------------------//

            try {
                if (p.waitFor() != 0) {
                    System.err.println("exit value = " + p.exitValue());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            bufferedreader.close();
            inread.close();
            buf.close();
            in.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return getOutput();


    }

    public abstract String getCommandStr();

    public void setOutput(String output) {
        this.output = output;
    }

    public String getOutput() {
        return output;
    }

    /**
     * @return the serverList
     */
    public ArrayList<Server> getServerList() {
        return serverList;
    }

    /**
     * @param serverList the serverList to set
     */
    public void setServerList(ArrayList<Server> serverList) {
        this.serverList = serverList;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }
}
